package oop;
import java.awt.*;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

//import java.sql.*;
import java.sql.*;
import java.util.Locale;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import com.toedter.calendar.JDateChooser;


public class Employee extends JPanel implements ActionListener{
          /**
         * 
         */
      private static final long serialVersionUID = 1L;
      private Font f;
      
      //Component of Super window
      private JPanel p;
      private JLabel labelHeader,labelSubheader,labelEmployeeCode,labelStartdate,labelName,labelSurname,labelGender,labelHomeNo,labelSoi,labelStreet,labelSubdistrict,labelDistrict,labelProvince,labelZipcode,labelTelNo,labelPic,labelPosition,labelChoose,labelStatus,labelStatusShow;
      private JTextField textEmployeeCode,textName,textSurname,textHomeNo,textSoi,textStreet,textSubdistrict,textDistrict,textProvince,textZipcode,textTelNo;
      private JComboBox<String> comboGender,comboPosition;
      private JButton buttonFind,buttonSave,buttonDelete,buttonBrowse;
      private JDateChooser calendar;
      int Width = 569, Height= 415;
      String  b = "" ,picAddress = "";
      String date = "";
      String gender = "";
      String address_number = "";
      String address_soi = "";
      String address_street = "";
      String address_subdistrict = "";
      String address_district = "";
      String address_province = "";
      String address_zipcode = "";
      String address_telephone_number = "";
      String status_show  = "";
      String status = "";
      
      //Component of Sub window
      private JPanel p_EmpFind;
      private JLabel EmpFindLabel_Header,EmpFindLabel_Search,EmpFindLabel_EmployeeCode,EmpFindLabel_Lastname,EmpFindLabel_Name;
      private JTextField EmpFindText_Name,EmpFindText_Lastname;
      private JComboBox<String> EmpFindText_EmployeeCode;
      private JButton EmpFindButton_ok,EmpFindButton_cancel;
      private JFrame FrameFind;
      
      //Component Of InsertUser Windows
      private Frame FrameLogin;
      private JPanel p_InsertUser;
      private JLabel labelCreateHeader,labelCreateSub,labelCreateUser,labelCreatePassword;
      private JTextField txtCreateUser,txtCreatePass;
      private JButton buttonCreateOk;
      
      public Employee(){
          setLayout(null);
          setBorder(BorderFactory.createTitledBorder(null, "Employee", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,new Font("Segoe UI",Font.PLAIN,14)));
          setBackground(new Color(238, 238, 238));
          setComponent();
          setBounds(10, 159, 569, 442);
  }
      public void setComponent(){
          p = new JPanel();
          p.setBorder(BorderFactory.createTitledBorder(null, "ที่อยู่", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION,new Font("Tahoma",Font.PLAIN,12)));
          p. setLayout(null);
          add(p);
          p.setBounds(50,130,290,229);
          p.setVisible(true);
          f = new Font("Tahoma",Font.PLAIN,12);
          
          labelHeader = new JLabel("EMPLOYEE");
          labelHeader.setFont(new Font("Segoe UI",Font.BOLD,18));
          labelSubheader = new JLabel("แสดงรายละเอียดและแก้ไขข้อมูลพนักงาน..");
          labelSubheader.setFont(new Font("Tahoma",Font.PLAIN,11));
          labelEmployeeCode = new JLabel("รหัสพนักงาน:");
          labelEmployeeCode.setFont(f);
          labelStartdate = new JLabel("วันที่เริ่มทำงาน:");
          labelStartdate.setFont(f);
          labelName = new JLabel("ชื่อ:");
          labelName.setFont(f);
          labelSurname = new JLabel("สกุล:");
          labelSurname.setFont(f);
          labelGender = new JLabel("เพศ:");
          labelGender.setFont(f);
          labelHomeNo = new JLabel("บ้านเลขที่:");
          labelHomeNo.setFont(f);
          labelSoi = new JLabel("ซอย:");
          labelSoi.setFont(f);
          labelStreet = new JLabel("ถนน:");
          labelStreet.setFont(f);
          labelSubdistrict = new JLabel("แขวง/ตำบล:");
          labelSubdistrict.setFont(f);
          labelDistrict = new JLabel("เขต/อำเภอ:");
          labelDistrict.setFont(f);
          labelZipcode = new JLabel("รหัสไปรษณีย์:");
          labelZipcode.setFont(f);
          labelProvince = new JLabel("จังหวัด:");
          labelProvince.setFont(f);
          labelTelNo = new JLabel("เบอร์โทรศัพท์:");
          labelTelNo.setFont(f);
          labelPic = new JLabel();
          labelPosition = new JLabel("ตำแหน่ง:");
          labelPosition.setFont(f);
          labelChoose = new JLabel("เลือกรูป:");
          labelChoose.setFont(f);
          labelStatus = new JLabel("สถานะ:");
          labelStatus.setFont(f);
          labelStatusShow = new JLabel("ปกติ");
          labelStatusShow.setFont(f);
          
          //set TextField
          
          textEmployeeCode = new JTextField();
          textEmployeeCode.setFont(f);
          textName = new JTextField();
          textName.setFont(f);
          textSurname = new JTextField();
          textSurname.setFont(f);
          textHomeNo = new JTextField();
          textHomeNo.setFont(f);
          textSoi = new JTextField();
          textSoi.setFont(f);
          textStreet = new JTextField();
          textStreet.setFont(f);
          textSubdistrict = new JTextField();
          textSubdistrict.setFont(f);
          textDistrict = new JTextField();
          textDistrict.setFont(f);
          textProvince = new JTextField();
          textProvince.setFont(f);
          textZipcode = new JTextField();
          textZipcode.setFont(f);
          textTelNo = new JTextField();
          textTelNo.setFont(f);
          
          buttonFind = new JButton("ค้นหา");             buttonFind.setFont(f);
          buttonFind.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          buttonFind.setIcon(new ImageIcon("D:\\Project\\src\\pic\\search.png"));
                
          buttonSave = new JButton("บันทึก");                   buttonSave.setFont(f);
          buttonSave.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          buttonSave.setIcon(new ImageIcon("D:\\Project\\src\\pic\\save.png"));
                
          buttonDelete = new JButton("ลบ");             buttonDelete.setFont(f); 
          buttonDelete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
          buttonDelete.setIcon(new ImageIcon("D:\\Project\\src\\pic\\delete.png"));
          
          buttonBrowse = new JButton("Browse..");       buttonBrowse.setFont(f); 
          buttonBrowse.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
              
                  
          calendar = new JDateChooser();
          calendar.setFont(f);
          //String day = ((JTextField)calendar.getDateEditor().getUiComponent()).getText();
          
          String Sex[] = {"ชาย","หญิง"};
          String Position[] = {"เจ้าของร้าน","พนักงาน"};
          comboGender = new JComboBox<String>(Sex);
          comboGender.setFont(f);
          comboGender.setBackground(new Color(238,238,238));
          
          comboPosition = new JComboBox<String>(Position);
          comboPosition.setFont(f);
          comboPosition.setBackground(new Color(238,238,238));
          
          add(labelHeader);
          add(labelSubheader);
          add(labelEmployeeCode);
          add(labelStartdate);
          add(labelName);
          add(labelSurname);
          add(labelGender);
          add(labelTelNo);
          add(labelPic);
          add(labelPosition);
          add(labelChoose);
          add(labelStatus);
          add(labelStatusShow);
          p.add(labelHomeNo);
          p.add(labelSoi);
          p.add(labelStreet);
          p.add(labelSubdistrict);
          p.add(labelDistrict);
          p.add(labelZipcode);
          p.add(labelProvince);
          
          add(textEmployeeCode);
          add(textName);
          add(textSurname);
          add(textTelNo);
          p.add(textHomeNo);
          p.add(textSoi);
          p.add(textStreet);
          p.add(textSubdistrict);
          p.add(textDistrict);
          p.add(textZipcode);
          p.add(textProvince);
          
          add(buttonFind);
          add(buttonSave);
          add(buttonDelete);
          add(buttonBrowse);
          
          add(comboGender);
          add(comboPosition);
          
          add(calendar);

          
          
  
          //a.setBounds(20, 10, 20, 20); 
          labelHeader.setBounds(20, 20, 150, 20);
          labelSubheader.setBounds(17, 45, 250, 15);
          labelEmployeeCode.setBounds(40, 75, 70, 15);
          labelStartdate.setBounds(240, 75, 85, 15);
          labelName.setBounds(40, 105, 30, 15);
          labelSurname.setBounds(195, 105, 30, 15);
          labelGender.setBounds(355, 105, 30, 15);
          labelHomeNo.setBounds(60, 32, 70, 15);
          labelSoi.setBounds(86, 57, 50, 15);
          labelStreet.setBounds(87, 82, 50, 15);
          labelSubdistrict.setBounds(49, 107, 100, 15);
          labelDistrict.setBounds(54, 132, 100, 15);
          labelProvince.setBounds(74, 157, 70, 15);
          labelZipcode.setBounds(43, 182, 100, 15);
          labelTelNo.setBounds(91, 380, 90, 15);
          
          labelPic.setBounds(390, 160, 150, 150);                       labelPic.setHorizontalAlignment(JLabel.CENTER);
          labelPosition.setBounds(370, 330, 90, 15);
          labelChoose.setBounds(373, 390, 90, 15);
          labelStatus.setBounds(378, 360, 90, 15);
          labelStatusShow.setBounds(433, 360, 90, 15);
  
          textEmployeeCode.setBounds(115,72,120,20);
          textName.setBounds(70, 102, 120, 20);
          textSurname.setBounds(230, 102, 120, 20);
          textHomeNo.setBounds(120,30,120,20);
          textSoi.setBounds(120,55,120,20);
          textStreet.setBounds(120,80,120,20);
          textSubdistrict.setBounds(120,105,120,20);
          textDistrict.setBounds(120,130,120,20);
          textProvince.setBounds(120,155,120,20);
          textZipcode.setBounds(120,180,120,20);
          textTelNo.setBounds(170, 378, 120, 20);
                  
  
          buttonFind.setBounds(460, 40, 85, 24);
          buttonSave.setBounds(460, 70, 85, 24);
          buttonDelete.setBounds(460, 100, 85, 24);
          buttonBrowse.setBounds(430, 388, 85, 24);
                 
          comboGender.setBounds(390, 102, 55, 20);
          comboPosition.setBounds(430, 330, 85, 20);
          
          calendar.setBounds(328, 73, 120, 20);
          calendar.setLocale(new Locale("th", "TH"));
          
          
                  
          Setup.setTextFieldNoInputNumber(textName,textSurname,textProvince,textSubdistrict,textDistrict);
          Setup.setTextFieldInputTel(textTelNo);
          Setup.setTextFieldInputNumber(textHomeNo);
          
          //action buttonSave panel Employee
          buttonSave.addActionListener(new ActionListener() {
  			
  			@Override
  			public void actionPerformed(ActionEvent e) {
  				// TODO Auto-generated method stub
 				if( textEmployeeCode.getText().equals("")||textName.getText().equals("")|| textSurname.getText().equals("")||textHomeNo.getText().equals("")|| textSoi.getText().equals("")||textStreet.getText().equals("")
                		  ||textSubdistrict.getText().equals("")||textDistrict.getText().equals("")||textProvince.getText().equals("")||textZipcode.getText().equals("")|| textTelNo.getText().equals("")||picAddress.equals("")){
 					JOptionPane.showMessageDialog(null, "กรุณากรอกข้อมูลให้ครบก่อน...", "Warning", JOptionPane.ERROR_MESSAGE);
                }
                else{
                	 try{
  						
                		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  						Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
  						Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  						ResultSet re = stmt.executeQuery("select EMPLOYEE_CODE from MSLM_MAIN_EMPLOYEE WHERE '"+textEmployeeCode.getText()+"'=EMPLOYEE_CODE");
  						Statement stmt2 = con.createStatement();
  						re.first();
  						
  						if(re.getRow() == 1){
  							int ans = JOptionPane.showConfirmDialog(null,"ต้องการแก้ไขข้อมูลใช่หรือไม่", "แก้ไข",JOptionPane.YES_NO_OPTION);
  			      			if (ans == 0){
  			      				stmt2.executeUpdate("update MSLM_MAIN_EMPLOYEE set EMPLOYEE_CODE = '"+textEmployeeCode.getText()
  																+"',EMPLOYEE_FIRST_NAME= '"+textName.getText()
  																+"',EMPLOYEE_LAST_NAME='"+textSurname.getText()
  																+"',EMPLOYEE_START_DATE='"+((JTextField)calendar.getDateEditor().getUiComponent()).getText()
  																+"',EMPLOYEE_GENDER='"+comboGender.getSelectedItem()
  																+"',EMPLOYEE_ADDRESS_NUMBER='"+textHomeNo.getText()
  																+"',EMPLOYEE_ADDRESS_SOI= '"+textSoi.getText()
  																+"',EMPLOYEE_ADDRESS_STREET='"+textStreet.getText()
  																+"',EMPLOYEE_ADDRESS_SUBDISTRICT='"+textSubdistrict.getText()
  																+"',EMPLOYEE_ADDRESS_DISTRICT='"+textDistrict.getText()
  																+"',EMPLOYEE_ADDRESS_PROVINCE='"+textProvince.getText()
  																+"',EMPLOYEE_ADDRESS_ZIPCODE='"+textZipcode.getText()
  																+"',EMPLOYEE_TELEPHONE_NUMBER='"+textTelNo.getText()
  																+"',EMPLOYEE_STATUS='"+labelStatusShow.getText()
  																+"',EMPLOYEE_ROLE='"+comboPosition.getSelectedItem()
  																+"',EMPLOYEE_PICTURE='"+picAddress
  																+"'where EMPLOYEE_CODE = '"+textEmployeeCode.getText()+"'") ;
  			      				JOptionPane.showMessageDialog(null,"แก้ไขข้อมูลเรียบร้อยแล้ว","Edit Confirm",JOptionPane.INFORMATION_MESSAGE);				      			
  			      			}
  						}
  						else{
  							//call Insert User Window
  	  						InsertUserLogin();	
  						}
  			      		
						re.close();
						stmt.close();
						stmt2.close();
						con.close();
  					}
                	 catch(SQLException s){
                		 JOptionPane.showMessageDialog(null, s.getMessage(), "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
                     }
                	 catch(ClassNotFoundException c){
                         JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
                     }
                }
 			}
  		});
          
          
          buttonDelete.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent arg0) {
                      // TODO Auto-generated method stub
            	 
            	  int ans = JOptionPane.showConfirmDialog(null,"ต้องการลบข้อมูลใช่หรือไม่", "ลบ",JOptionPane.YES_NO_OPTION);
      			if (ans == 0) {
      				Statement stmt;
    				Connection con;
      			try 
                 {
      			 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				 con = DriverManager.getConnection("jdbc:odbc:MSL");
				 stmt = con.createStatement();
				 stmt.executeUpdate("update MSLM_MAIN_EMPLOYEE set EMPLOYEE_STATUS ='ลาออก' where EMPLOYEE_CODE = '"+textEmployeeCode.getText()+"'");
				 stmt.close();
				 con.close();
				 
				 JOptionPane.showMessageDialog(null,"ลบข้อมูลเรียบร้อยแล้ว","Delete Confirm",JOptionPane.INFORMATION_MESSAGE);
				
                 
      			Setup.ClearTextField(textEmployeeCode,textName,textSurname,textHomeNo,textSoi,textStreet,textSubdistrict,textDistrict,textProvince,textZipcode,textTelNo);
				comboGender.setSelectedItem(0);
				comboPosition.setSelectedItem(0);
				labelPic.setIcon(null);     
				calendar.setDate(null);
				labelStatusShow.setText("ปกติ");
				picAddress = "";
      			}
				
             	 catch(SQLException s){
                      JOptionPane.showMessageDialog(null, s.getMessage(), "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
              }
              catch(ClassNotFoundException c){
                      JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
              }
      			}
              }
        });
          
          
          buttonBrowse.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent arg0) {
                      // TODO Auto-generated method stub
            	  FileDialog f = new FileDialog(new Frame());
                  f.setVisible(true);
                  
                  // if not select picture at first.It will not show.
                  if(f.getDirectory() == null){
                	  if(b.equals(""))
                		  labelPic.setIcon(new ImageIcon(b));
                  }
                  else{
                	  String pic = f.getDirectory()+f.getFile();
                	  picAddress = pic.replace("\\",    "\\\\");
                	  b = picAddress;
                	  labelPic.setIcon(new ImageIcon(picAddress));
                  }
              }
          });
              
          buttonFind.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                        // TODO Auto-generated method stub
                        EmployeeFind();
                        
                        
                        
                }
          });
      }
      
      
      
      public void EmployeeFind(){
    	  
    	  FrameFind = new JFrame("EmployeeFind");
    	  FrameFind.setLayout(null);
          p_EmpFind = new JPanel(null);
          p_EmpFind.setBorder(BorderFactory.createTitledBorder(null, "Employee Find", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,new Font("Segoe UI",Font.PLAIN,14)));
          
          
          //setComponent
          
          //Create
          EmpFindLabel_Header = new JLabel("EMPLOYEE FIND");
          EmpFindLabel_Search = new JLabel("ค้นหาพนักงาน..");            
          EmpFindLabel_EmployeeCode = new JLabel("รหัสพนักงาน:");               
          EmpFindLabel_Name = new JLabel("ชื่อ:");              
          EmpFindLabel_Lastname = new JLabel("สกุล:");                          
          EmpFindText_EmployeeCode = new JComboBox<String>();           
          EmpFindText_Name = new JTextField() ;          
          EmpFindText_Lastname= new JTextField() ;               
          EmpFindButton_ok = new JButton("ตกลง");
          EmpFindButton_cancel = new JButton("ยกเลิก");
                  
          //Set font
          EmpFindLabel_Header.setFont(new Font("Segoe UI",Font.BOLD,18));
          EmpFindLabel_Search.setFont(new Font("Tahoma",Font.PLAIN,11));
          EmpFindLabel_EmployeeCode.setFont(f);
          EmpFindLabel_Name.setFont(f);
          EmpFindLabel_Lastname.setFont(f);
          EmpFindText_EmployeeCode.setFont(f);
          EmpFindText_Name.setFont(f);
          EmpFindText_Lastname.setFont(f);
          EmpFindButton_ok.setFont(new Font("Tahoma",Font.PLAIN,11));
          EmpFindButton_cancel.setFont(new Font("Tahoma",Font.PLAIN,11));
          
          //Add
          FrameFind.add(EmpFindLabel_Header);
          FrameFind.add(EmpFindLabel_Search);
          FrameFind.add(EmpFindLabel_EmployeeCode);
          FrameFind.add(EmpFindLabel_Name);
          FrameFind.add(EmpFindLabel_Lastname);
          FrameFind.add(EmpFindText_EmployeeCode);
          FrameFind.add(EmpFindText_Name);
          FrameFind.add(EmpFindText_Lastname);
          FrameFind.add(EmpFindButton_ok);
          FrameFind.add(EmpFindButton_cancel);
          
          //setBounds
          EmpFindLabel_Header.setBounds(20, 25, 150, 20);
          EmpFindLabel_Search.setBounds(20, 45, 90, 20);
          EmpFindLabel_EmployeeCode.setBounds(65, 70, 90, 20);
          EmpFindLabel_Name.setBounds(110, 105, 90, 20);
          EmpFindLabel_Lastname.setBounds(105, 140, 90, 20);
          EmpFindText_EmployeeCode.setBounds(140, 75, 125, 20);
          EmpFindText_Name.setBounds(140, 110, 125, 22);
          EmpFindText_Lastname.setBounds(140, 145, 125, 20);     
          EmpFindButton_ok.setBounds(280, 75,85,25);
          EmpFindButton_cancel.setBounds(280, 105,85,25);
          
          //setIcon
          EmpFindButton_ok.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\ok.png"));
          EmpFindButton_cancel.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\Delete.png"));
          
          //end setComponent
          
          p_EmpFind.setBounds(9, 5, 383, 225);
          FrameFind.add(p_EmpFind);
          FrameFind.setBounds(455,160,409,272);
          FrameFind.setResizable(false);
          FrameFind.setVisible(true);
          
          //count size array
          String EmployeeSize[] = new String[Setup.CountDATA("select COUNT(EMPLOYEE_CODE) AS CountCode from MSLM_MAIN_EMPLOYEE", "CountCode")];
	    	 
			try{
	  			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	  			Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
	  			Statement stmt,stmt1;
	  			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
	  			stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
	  			
	  			ResultSet re,re1;
	  			re1 = stmt1.executeQuery("select EMPLOYEE_CODE,EMPLOYEE_FIRST_NAME,EMPLOYEE_LAST_NAME from MSLM_MAIN_EMPLOYEE where EMPLOYEE_ID = 1");
    			re1.first();
	  			for(int i = 0;i < EmployeeSize.length;i++){
		    			re = stmt.executeQuery("select EMPLOYEE_CODE,EMPLOYEE_FIRST_NAME,EMPLOYEE_LAST_NAME from MSLM_MAIN_EMPLOYEE where EMPLOYEE_ID = "+(i+1)+"");
		    			re.first();
		    			EmployeeSize[i] = re.getString("EMPLOYEE_CODE");
		    			re.close();
	  			}
	  			//set data
	  			EmpFindText_Name.setText(re1.getString("EMPLOYEE_FIRST_NAME"));
    			EmpFindText_Lastname.setText(re1.getString("EMPLOYEE_LAST_NAME"));
    			re1.close();
    			con.close();
    			EmpFindText_EmployeeCode.addItem(" ");
    			if(EmpFindText_EmployeeCode.getSelectedIndex() == 0){
    				EmpFindText_Name.setText(" ");
    				EmpFindText_Lastname.setText(" ");
    			}
	  			for(int i = 0;i < EmployeeSize.length;i++){
	  				EmpFindText_EmployeeCode.addItem(EmployeeSize[i]);
	  			}
	  		}
	  		catch(SQLException s){
	  			JOptionPane.showMessageDialog(null, "SQL_ERROR", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
	  		}
	  		catch(ClassNotFoundException c){
	  			JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
	  		}
	  		//add action
	  		EmpFindButton_ok.addActionListener(this);	
	  		EmpFindButton_cancel.addActionListener(this);
          	EmpFindText_EmployeeCode.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if(EmpFindText_EmployeeCode.getSelectedIndex() == 0){
					Setup.ClearTextField(EmpFindText_Name,EmpFindText_Lastname);
				}
				else{
					try{
			  			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			  			Connection con = DriverManager.getConnection("jdbc:odbc:MSL");
			  			Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			  			ResultSet re;
			  			
				    			re = stmt.executeQuery("select EMPLOYEE_CODE,EMPLOYEE_FIRST_NAME,EMPLOYEE_LAST_NAME,EMPLOYEE_START_DATE,EMPLOYEE_GENDER,EMPLOYEE_ADDRESS_NUMBER,EMPLOYEE_ADDRESS_SOI,EMPLOYEE_ADDRESS_STREET,EMPLOYEE_ADDRESS_SUBDISTRICT,EMPLOYEE_ADDRESS_DISTRICT,EMPLOYEE_ADDRESS_PROVINCE,EMPLOYEE_ADDRESS_ZIPCODE,EMPLOYEE_TELEPHONE_NUMBER,EMPLOYEE_STATUS,EMPLOYEE_ROLE,EMPLOYEE_PICTURE from MSLM_MAIN_EMPLOYEE where EMPLOYEE_CODE = '"+EmpFindText_EmployeeCode.getSelectedItem()+"'");
				    			re.first();
				    		
				    			EmpFindText_Name.setText(re.getString("EMPLOYEE_FIRST_NAME"));
				    			EmpFindText_Lastname.setText(re.getString("EMPLOYEE_LAST_NAME"));
				    			date = re.getString("EMPLOYEE_START_DATE");
				    			gender = re.getString("EMPLOYEE_GENDER");
				    			address_number = re.getString("EMPLOYEE_ADDRESS_NUMBER");
				    			address_soi = re.getString("EMPLOYEE_ADDRESS_SOI");
				    			address_street = re.getString("EMPLOYEE_ADDRESS_STREET");
				    			address_subdistrict = re.getString("EMPLOYEE_ADDRESS_SUBDISTRICT");
				    			address_district = re.getString("EMPLOYEE_ADDRESS_DISTRICT");
				    			address_province = re.getString("EMPLOYEE_ADDRESS_PROVINCE");
				    			address_zipcode = re.getString("EMPLOYEE_ADDRESS_ZIPCODE");
				    			address_telephone_number = re.getString("EMPLOYEE_TELEPHONE_NUMBER");
				    			status_show = re.getString("EMPLOYEE_STATUS");
				    			status = re.getString("EMPLOYEE_ROLE");
				    			picAddress = re.getString("EMPLOYEE_PICTURE");
				    			re.close();
				    			con.close();
			  			
			  		}
			  		catch(SQLException s){
			  			JOptionPane.showMessageDialog(null, "SQL_ERROR3", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
			  		}
			  		catch(ClassNotFoundException c){
			  			JOptionPane.showMessageDialog(null, "CLASS_NOTFOUND", "MESSAGE_ERROR", JOptionPane.ERROR_MESSAGE);
			  		}
				}
			}
			
		});
      }
      
      public void InsertUserLogin(){
    	  FrameLogin = new Frame("CreateUserLogin");
    	  FrameLogin.setBackground(new Color(238, 238, 238));
    	  FrameLogin.setLayout(null);
    	  
    	  p_InsertUser = new JPanel(null);
          p_InsertUser.setBorder(BorderFactory.createTitledBorder(null, "CreateUserLogin", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,new Font("Segoe UI",Font.PLAIN,14)));
          p_InsertUser.setBounds(9,30, 383, 160);
          
          //component
          labelCreateHeader = new JLabel("CREATE USER");
          labelCreateHeader.setFont(new Font("Segoe UI",Font.BOLD,18));
          labelCreateSub = new JLabel("กำหนดชื่อและรหัสผ่าน..");
          labelCreateSub.setFont(new Font("Tahoma",Font.PLAIN,11));
          labelCreateUser = new JLabel("Username :");
          labelCreateUser.setFont(new Font("Tahoma",Font.PLAIN,11));
          labelCreatePassword = new JLabel("Password :");
          labelCreatePassword.setFont(new Font("Tahoma",Font.PLAIN,11));
          txtCreateUser = new JTextField();
          txtCreatePass = new JTextField();
          buttonCreateOk = new JButton("ตกลง");
          
          //add component
          p_InsertUser.add(labelCreateHeader);
          p_InsertUser.add(labelCreateSub);
          p_InsertUser.add(labelCreateUser);
          p_InsertUser.add(labelCreatePassword);
          p_InsertUser.add(txtCreateUser);
          p_InsertUser.add(txtCreatePass);
          p_InsertUser.add(buttonCreateOk);			buttonCreateOk.setFont(new Font("Tahoma",Font.PLAIN,11));
          
          //setBounds Component
          labelCreateHeader.setBounds(20, 20, 150, 20);
          labelCreateSub.setBounds(20, 45, 90, 20);
          labelCreateUser.setBounds(65, 70, 90, 20);
          labelCreatePassword.setBounds(65, 97, 90, 20);
          txtCreateUser.setBounds(125, 70, 125, 20);
          txtCreatePass.setBounds(125, 97, 125, 20);
          buttonCreateOk.setBounds(280, 70,85,25);
          
          //setIcon
          buttonCreateOk.setIcon(new javax.swing.ImageIcon("D:\\Project\\src\\pic\\ok.png"));
          
          FrameLogin.add(p_InsertUser);
          FrameLogin.setBounds(455,160,400,198);
          FrameLogin.setResizable(false);
          FrameLogin.setVisible(true);
          
          buttonCreateOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Connection con;//connect to database
				//stmt select LOGIN_NAME
				//stmt1 insert into MSLM_MAIN_USER_LOGIN
				//stmt2 insert into MSLT_MAIN_USER_PRIVILEGE
				//stmt3 INSERT INTO MSLM_MAIN_EMPLOYEE
				Statement stmt,stmt1,stmt2,stmt3;
				
				int ans = JOptionPane.showConfirmDialog(null,"ต้องการเพิ่มข้อมูลใช่หรือไม่", "เพิ่ม",JOptionPane.YES_NO_OPTION);
				// if ans = 0 confirm to add data to base
      			//start windows confirm
				if (ans == 0){
					try{
						Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
						con = DriverManager.getConnection("jdbc:odbc:MSL");
						stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
						ResultSet re = stmt.executeQuery("select LOGIN_NAME from MSLM_MAIN_USER_LOGIN where LOGIN_NAME = '"+txtCreateUser.getText()+"'");
						re.first(); //select first row in database
						
						if(re.getRow() == 0){	//if have not row
							stmt1 = con.createStatement();
							stmt1.executeUpdate("insert into MSLM_MAIN_USER_LOGIN(LOGIN_NAME,LOGIN_PASSWORD)values('"+txtCreateUser.getText()+"','"+txtCreatePass.getText()+"')");
							stmt2 = con.createStatement();
							if(comboPosition.getSelectedItem().equals("เจ้าของร้าน"))
								stmt2.executeUpdate("insert into MSLT_MAIN_USER_PRIVILEGE(STAMP_PRIVILEGE)values('t')");
							else if(comboPosition.getSelectedItem().equals("พนักงาน"))
								stmt2.executeUpdate("insert into MSLT_MAIN_USER_PRIVILEGE(STAMP_PRIVILEGE)values('f')");
			      			stmt3 = con.createStatement();
							stmt3.executeUpdate("INSERT INTO MSLM_MAIN_EMPLOYEE(EMPLOYEE_CODE" +
      							",EMPLOYEE_FIRST_NAME," +
      							"EMPLOYEE_LAST_NAME," +
      							"EMPLOYEE_START_DATE," +
      							"EMPLOYEE_GENDER," +
      							"EMPLOYEE_ADDRESS_NUMBER," +
      							"EMPLOYEE_ADDRESS_SOI," +
      							"EMPLOYEE_ADDRESS_STREET," +
      							"EMPLOYEE_ADDRESS_SUBDISTRICT," +
      							"EMPLOYEE_ADDRESS_DISTRICT," +
      							"EMPLOYEE_ADDRESS_PROVINCE," +
      							"EMPLOYEE_ADDRESS_ZIPCODE," +
      							"EMPLOYEE_TELEPHONE_NUMBER," +
      							"EMPLOYEE_STATUS," +
      							"EMPLOYEE_ROLE," +
      							"EMPLOYEE_PICTURE)values(" +
      							"'"+textEmployeeCode.getText()+"'," +
								"'"+textName.getText()+"'," +
								"'"+textSurname.getText()+"'," +
								"'"+((JTextField)calendar.getDateEditor().getUiComponent()).getText()+"'," +
								"'"+comboGender.getSelectedItem()+"'," +
								"'"+textHomeNo.getText()+"'," +
								"'"+textSoi.getText()+"'," +
								"'"+textStreet.getText()+"'," +
								"'"+textSubdistrict.getText()+"'," +
								"'"+textDistrict.getText()+"'," +
								"'"+textProvince.getText()+"'," +
								"'"+textZipcode.getText()+"'," +
								"'"+textTelNo.getText()+"'," +
								"'"+labelStatusShow.getText()+"'," +
								"'"+comboPosition.getSelectedItem()+"'," +
			      				"'"+picAddress+"')");
							
							
							JOptionPane.showMessageDialog(null,"เพิ่มข้อมูลเรียบร้อยแล้ว","Edit Confirm",JOptionPane.INFORMATION_MESSAGE);
							
							//clear textBox and combobox in main windows
							Setup.ClearTextField(textEmployeeCode,textName,textSurname,textHomeNo,textSoi,textStreet,textSubdistrict,textDistrict,textProvince,textZipcode,textTelNo);
							comboGender.setSelectedIndex(0);
							comboPosition.setSelectedIndex(0);
							labelPic.setIcon(null);     
							calendar.setDate(null);
							stmt3.close();
							stmt2.close();
							stmt1.close();
							
							//close windows insertLoginUser
							FrameLogin.dispose();
						}
						else if(re.getRow() != 0)
							JOptionPane.showMessageDialog(null, "ชื่อและรหัสผ่านซ้ำกรุณากรอกใหม่ครับ...","ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
						stmt.close();
						con.close();
					}
					catch(SQLException e){
						JOptionPane.showMessageDialog(null, e.getMessage());
					}
					catch(ClassNotFoundException e){
						JOptionPane.showMessageDialog(null, e.getMessage());
					}							
				}//end if ans
			}//end actionPerformed
          });//end function Listenner 
      }
      
      //function action ButtonFind
      public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
    	  
    	  //action button of Find windows
    	  if(e.getSource().equals(EmpFindButton_ok)){
    		  if(EmpFindText_EmployeeCode.getSelectedIndex() == 0){
					JOptionPane.showMessageDialog(null, "โปรดเลือกก่อนครับ...","Question",JOptionPane.QUESTION_MESSAGE);
    		  }
    		  else{
    			textEmployeeCode.setText(EmpFindText_EmployeeCode.getSelectedItem().toString());
  				textName.setText(EmpFindText_Name.getText());
  				textSurname.setText(EmpFindText_Lastname.getText());
  				((JTextField)calendar.getDateEditor().getUiComponent()).setText(date);
				//set Text Color Calendar
		        ((JTextField)calendar.getDateEditor().getUiComponent()).setForeground(Color.BLACK);
    			comboGender.setSelectedItem(gender);
				textHomeNo.setText(address_number);
				textSoi.setText(address_soi);
				textStreet.setText(address_street); 
				textSubdistrict.setText(address_subdistrict); 
				textDistrict.setText(address_district); 
				textProvince.setText(address_province);
				textZipcode.setText(address_zipcode); 
				textTelNo.setText(address_telephone_number);
				labelStatusShow.setText(status_show);
    			comboPosition.setSelectedItem(status);
  				labelPic.setIcon(new ImageIcon(picAddress));

				FrameFind.dispose();
    		  }
    	  }
    	  else if(e.getSource().equals(EmpFindButton_cancel)){
    		  FrameFind.dispose();
    	  }
		}
}
